using System;
using Firestarter.Test.Base;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;
using NUnit.Framework;

namespace FireStarterModeller.Test.BO
{
    /// <summary>
    /// Provides a place to write custom tests for DMClass objects.
    /// This file is only written once and can be changed.  The Def file
    /// attached to this as a dependent is rewritten with each regeneration
    /// and contains the standard tests for DMClass.
    /// Regenerate this test project whenever there have been changes to the
    /// business objects.
    /// If tests are failing due to a unique setup in your application,
    /// you can either override the Create methods in TestUtils, or you
    /// can add the test to the ignore list below and reimplement it here.
    /// </summary>
    [TestFixture]
    public class TestDMClassDuplicateUIViewNameRule
    {
        [SetUp]
        public void SetupTest()
        {
            //Runs every time that any testmethod is executed
            TestUtilsDomain.SetupTest();
            BORegistry.DataAccessor = new DataAccessorInMemory();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //Code that is executed before any test is run in this class. If multiple tests
            // are executed then it will still only be called once.
            TestUtilsDomain.SetupFixture();
        }

        [TearDown]
        public void TearDownTest()
        {
            //runs every time any testmethod is complete
        }

        #region DMClassDuplicateUIViewName

        [Test]
        public void Test_CreateDMClassDuplicateUIViewNameRule()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IBusinessObjectRule rule = new DMClassDuplicateUIViewNameRule();
            //---------------Test Result -----------------------
            Assert.IsNotNull(rule);
            Assert.AreEqual("DMClassDuplicateUIViewName", rule.Name);
            Assert.AreEqual(ErrorLevel.Warning, rule.ErrorLevel);

        }

 /*       [Test]
        public void Test_CreateDMClassDuplicateUIViewNameRule_WhenClassNull_ShouldRaiseError()
        {
            //---------------Set up test pack-------------------
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            try
            {
                new DMClassDuplicateUIViewNameRule(null);
                Assert.Fail("expected ArgumentNullException");
            }
            //---------------Test Result -----------------------
            catch (ArgumentNullException ex)
            {
                StringAssert.Contains("Value cannot be null", ex.Message);
                StringAssert.Contains("dmClass", ex.ParamName);
            }
        }*/

        [Test]
        public void Test_IsValid_False_WhenDuplicateUIViewName()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            UIView uiView1 = dmClass.UIViews.CreateBusinessObject();
            uiView1.ViewName = TestUtilsShared.GetRandomString();
            UIView uiView2 = dmClass.UIViews.CreateBusinessObject();
            uiView2.ViewName = uiView1.ViewName;
            IBusinessObjectRule rule = new DMClassDuplicateUIViewNameRule();
            //---------------Assert Precondition----------------
            Assert.AreEqual(uiView1.ViewName, uiView2.ViewName);
            Assert.AreEqual(2, dmClass.UIViews.Count);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmClass);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
        }

        [Test]
        public void Test_IsValid_True_WhenNoUIViews()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            IBusinessObjectRule rule = new DMClassDuplicateUIViewNameRule();
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, dmClass.UIViews.Count);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmClass);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_IsValid_True_WhenOneUIView()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            dmClass.UIViews.CreateBusinessObject();
            IBusinessObjectRule rule = new DMClassDuplicateUIViewNameRule();
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, dmClass.UIViews.Count);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmClass);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_IsValid_True_WhenNoDuplicateUIViewName()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            UIView uiView1 = dmClass.UIViews.CreateBusinessObject();
            uiView1.ViewName = TestUtilsShared.GetRandomString();
            UIView uiView2 = dmClass.UIViews.CreateBusinessObject();
            uiView2.ViewName = TestUtilsShared.GetRandomString();
            IBusinessObjectRule rule = new DMClassDuplicateUIViewNameRule();
            //---------------Assert Precondition----------------
            Assert.AreNotEqual(uiView1.ViewName, uiView2.ViewName);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmClass);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_IsValid_True_WhenUIView_ViewNameEmpty()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            UIView uiView1 = dmClass.UIViews.CreateBusinessObject();
            uiView1.ViewName = TestUtilsShared.GetRandomString();
            UIView uiView2 = dmClass.UIViews.CreateBusinessObject();
            uiView2.ViewName = "";
            IBusinessObjectRule rule = new DMClassDuplicateUIViewNameRule();
            //---------------Assert Precondition----------------
            Assert.AreNotEqual(uiView1.ViewName, uiView2.ViewName);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmClass);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_IsValid_False_WhenDuplicateUIViewName_AlsoHasUIViewNameEmpty()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            UIView uiView1 = dmClass.UIViews.CreateBusinessObject();
            UIView uiView2 = dmClass.UIViews.CreateBusinessObject();
            dmClass.UIViews.CreateBusinessObject();
            string viewName = TestUtilsShared.GetRandomString();
            uiView1.ViewName = viewName;
            uiView2.ViewName = viewName;
            IBusinessObjectRule rule = new DMClassDuplicateUIViewNameRule();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmClass);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
        }

        [Test]
        public void Test_IsValid_False_WhenManyDuplicateUIViewName()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            UIView uiView1 = dmClass.UIViews.CreateBusinessObject();
            UIView uiView2 = dmClass.UIViews.CreateBusinessObject();
            UIView uiView3 = dmClass.UIViews.CreateBusinessObject();
            string viewName = TestUtilsShared.GetRandomString();
            uiView1.ViewName = viewName;
            uiView2.ViewName = TestUtilsShared.GetRandomString();
            uiView3.ViewName = viewName;
            IBusinessObjectRule rule = new DMClassDuplicateUIViewNameRule();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmClass);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            StringAssert.Contains("The Entity/Class '", rule.Message);
            StringAssert.Contains("'" + dmClass.ToString() + "'", rule.Message);
            StringAssert.Contains("' cannot have more than one UIView with the same name('", rule.Message);
        }

        [Test]
        public void Test_IsValid_False_WhenTwoDuplicateUIViewName()
        {
            //---------------Set up test pack-------------------
            DMAssembly assembly = TestUtilsShared.CreateDMAssembly();
            DMClass dmClass = assembly.Classes.CreateBusinessObject();
            dmClass.ClassNameBO = TestUtilsShared.GetRandomString();
//            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            UIView uiView1 = dmClass.UIViews.CreateBusinessObject();
            UIView uiView2 = dmClass.UIViews.CreateBusinessObject();
            string viewName = TestUtilsShared.GetRandomString();
            uiView1.ViewName = viewName;
            uiView2.ViewName = viewName;
            dmClass.Save();
            IBusinessObjectRule rule = new DMClassDuplicateUIViewNameRule();
            //---------------Assert Precondition----------------
            Assert.AreEqual(uiView1.ViewName,uiView2.ViewName);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmClass);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            StringAssert.Contains("The Entity/Class '", rule.Message);
            StringAssert.Contains("'" + dmClass.ToString() + "'", rule.Message);
            StringAssert.Contains("' cannot have more than one UIView with the same name('", rule.Message);
        }
        #endregion
    }

}